<?php
/**
 * Created by PhpStorm.
 * User: yuyiwen
 * Date: 2018/10/26
 * Time: 上午9:45
 */

namespace app\admin\controller;


use think\Request;

class Member extends Common
{
    public function index ()
    {
        // 判断用户是否有搜索
        $search = input('get.search' , '' , 'htmlspecialchars');
        $ad = db('member');
        $config = [];
        if ($search != '')
        {
            $config = ['query'=>['search' =>$search ]];
            if (session('admin_id') == '1') {
                $ad->whereLike('numbers' , "%{$search}%" , 'or');
            }
            $ad
                ->whereLike('name' , "%{$search}%" , 'or')
                ->whereLike('phone' , "%{$search}%",'or');
        }
        if (session('admin_id') != '1') {
            $ad->where('numbers' , session('tuijian_id'));
        }

        $ad_list =$ad->paginate(10 , false , $config);
        $page  = $ad_list->render();
        $ad_list = $ad_list->toArray();

        foreach ($ad_list['data'] as $k => $v) {
            $ad_list['data'][$k]['expire_time'] = date('Y-m-d H:i:s' , $v['expire_time']);
            $ad_list['data'][$k]['type'] = $v['type'] == 1 ? 'qq' : '微信';
        }

        $search = ['pagination' , 'disabled' , 'active'];
        $replace = ['am-pagination tpl-pagination' , 'am-disabled' , 'am-active'];
        $page = str_replace($search , $replace , $page);

        $request = Request::instance();
        if ($request -> isAjax()) {
            return json(['ad_list' => $ad_list , 'page' => $page]);
        }else {
            $this->assign('page', $page);
            $this->assign('ad_list', $ad_list);
            return view();
        }
    }

    public function addUser ()
    {
        $post = input('post.');
        $post['expire_time'] = time() + ($post['expire_time'] * 60 *60 * 24);
        $post['type'] = $post['type'] == 2 ? 2 : 1;
        db('member')->insertGetId($post);
    }
    public function delete ()
    {
        $id = input('id');
        db('member')->delete($id);
    }

    public function xuqi ()
    {
        $post = input('post.');
        $info = db('member')->find($post['id']);
        if ($info) {
            $post['expire_time'] = $info['expire_time'] +  ($post['expire_time'] * 60 *60 * 24);
        }
        db('member')->update($post);
    }

    public function bianji ()
    {
        $post = input('post.');
        db('member_data_type')->where('member_id' , $post['id'])->delete();
        foreach ($post['data_type'] as $key => $value) {
            db('member_data_type') -> insert(['data_type'=>$value , 'member_id'=> $post['id']]);
        }
    }

    public function getbianji ()
    {
        $get = input('get.');
        $ids = db('member_data_type')->where('member_id' , $get['id'])->select();
        $return = '';
        if ($ids) {
            foreach ($ids as $key => $value) {
                $return .= $value['data_type'] . ',';
            }
            $return = trim($return , ',');
            return $return;
        }else {
            return '';
        }
    }

}